package com.wisdomparking.tools;

import com.google.gson.Gson;
import com.wisdomparking.bean.Parking;
import com.wisdomparking.bean.Rawdata;
import com.wisdomparking.bean.RealTimeData;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.RawdataService;

public class ToolService {
	
	/**
	 * 通过 App 支付完成后通知车场做抬杠动作【>】
	 * 
	 * @param identification	车场唯一标识
	 * @param plateNumber		车牌号
	 * @return
	 */
	public static boolean payfinishedSendParking(ParkingService parkingService, String identification, String plateNumber){
		boolean flag = false;
		
		if(ToolValid.isNotEmpty(identification)){
			Parking parking = parkingService.getParkingByIdentification(identification);
			
			if(ToolValid.isNotNull(parking)){
				String queryString = "identification=" + identification + "&plateNumber=" + plateNumber;
				ToolString.doGet(parking.getNoticeUrl(), queryString, "utf-8", true);
				flag = true;
			}
		}
		
		return flag;
	}
	
	/**
	 * 实时数据入库
	 * 
	 * @param parkingService	
	 * @param identification	车场唯一标识
	 * @return
	 */
	public static boolean dataStorage(RawdataService rawdataService, ParkingService parkingService, String identification){
		boolean flag = false;
		
		if(ToolValid.isNotEmpty(identification)){
			Rawdata rawdata = rawdataService.getRawdataByIdentification(identification);
			
			if(ToolValid.isNotNull(rawdata) && ToolValid.isNotEmpty(rawdata.getRealTimeData())){
				RealTimeData realTimeData = new Gson().fromJson(rawdata.getRealTimeData(), RealTimeData.class);
				Parking parking = parkingService.getParkingByIdentification(identification);
				
				if(ToolValid.isNotNull(parking)){
					
					if(ToolValid.isEmpty(rawdata.getPutInStorageMode()) || "realTimeData".equals(rawdata.getPutInStorageMode())){
						parking.setSurplusLargeVehicleA(realTimeData.getSurplusLargeVehicleA());
						parking.setSurplusLightdutyVehicleA(realTimeData.getSurplusLightdutyVehicleA());
						parking.setSurplusLargeVehicleB(realTimeData.getSurplusLargeVehicle());
						parking.setSurplusLightdutyVehicleB(realTimeData.getSurplusLightdutyVehicle());
						
						flag = parkingService.updateParking(parking, false);
						if(flag){
							rawdata.setPutInStorageMark(true);
							flag = rawdataService.updateRawdata(rawdata, false);
						}
					}
					
				}
				
			}
			
		}
		
		return flag;
	}
	
}
